import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-10-19
 * Time: 10:59
 */
public class Test3 {
    public static int LastRemaining_Solution (int n, int m) {
        // write code here
        LinkedList<Integer> list = new LinkedList<>();
        for(int i = 0; i < n; i++) {
            list.add(i);
        }
        int index = 0;
        while(list.size() != 1) {
            int rem = (index +m - 1) % list.size();
            list.remove(rem);
            index = rem;
        }
        return list.getFirst();
    }

    public static void main(String[] args) {
        int ret = LastRemaining_Solution(10, 17);
        System.out.println(ret);
    }
}
